package IndexingLucene;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

import Classes.Path;

public class PreProcessedCorpusReader {
	
	private BufferedReader br;
	private FileInputStream fis;
	private InputStreamReader isr;

	/**
	 * This constructor should open the file in Path.DataTextDir and also should 
	 * make preparation for function nextDocument(). 
	 * Remember to close the file that you opened, when you do not use it any more
	 * 
	 * @param type
	 * @throws IOException
	 */
	public PreProcessedCorpusReader(String type) throws IOException {
		fis = new FileInputStream(Path.ResultHM1 + type);
		isr = new InputStreamReader(fis);
        br = new BufferedReader(isr);   
	}
	
	/**
	 * Get document one by one.
	 * 
	 * @return
	 * @throws IOException
	 */
	public Map<String, Object> nextDocument() throws IOException {
		String docno = br.readLine();
		if(docno == null) {
			fis.close();
			isr.close();
			br.close();
			return null;
		}
		String content = br.readLine();
		
		Map<String, Object> doc = new HashMap<>();
		doc.put(docno, content.toCharArray());
		return doc;
	}
}
